package com.greate.community.constant;

import org.springframework.lang.Nullable;

import java.util.Objects;

public enum ExpType {

    // 事件触发经验
    EXP_LOGIN(5, LoopModes.DAY, 5, "login", "每日首次登录"),
    EXP_BROWSE(5, LoopModes.DAY, 5, "browse", "每日首次浏览"),
    EXP_SHARE(5, LoopModes.DAY, 5, "share", "每日首次分享"),
    EXP_LIKE(5, LoopModes.DAY, 25, "like", "点赞帖子"),
    EXP_COMMENT(5, LoopModes.DAY, 25, "comment", "发表评论"),
    EXP_PUBLISH(10, LoopModes.DAY, 30, "publish", "发表帖子"),
    // 转化经验
    EXP_POST_LIKE_COUNT(10, LoopModes.ONLY_ONE, 0, "postLikeCount", "帖子点赞数");

    static int ONE_LEVEL_OF_EXP = 0; // 通过实名认证
    static int TWO_LEVEL_OF_EXP = 200;
    static int THREE_LEVEL_OF_EXP = 1000;
    static int FOUR_LEVEL_OF_EXP = 2333;
    static int FIVE_LEVEL_OF_EXP = 6666;
    static int SIX_LEVEL_OF_EXP = 16666;

    private static final ExpType[] TYPES = values();
    private final int count;
    private final ExpType.LoopModes loopMode;
    private final int limit;
    private final String name;
    private final String content;

    private ExpType(int count, ExpType.LoopModes loopMode, int limit, String name, String content) {
        this.count = count;
        this.loopMode = loopMode;
        this.limit = limit;
        this.name = name;
        this.content = content;
    }

    public int getCount() {
        return this.count;
    }

    public ExpType.LoopModes getLoopMode() {
        return this.loopMode;
    }

    public int getLimit() {
        return this.limit;
    }

    public String getName() {
        return this.name;
    }

    public String getContent() {
        return this.content;
    }

    /**
     * 根据名称遍历查找经验类型
     *
     * @param expTypeName
     * @return
     */
    @Nullable
    public static ExpType resolve(String expTypeName) {
        for (ExpType expType : TYPES) {
            if (Objects.equals(expType.name, expTypeName)) {
                return expType;
            }
        }

        return null;
    }

    /**
     * 根据经验值判断等级
     *
     * @param totalExp
     * @return
     */
    public static int getLevel(int totalExp) {
        if (totalExp >= SIX_LEVEL_OF_EXP) {
            return 6;
        } else if (totalExp >= FIVE_LEVEL_OF_EXP) {
            return 5;
        } else if (totalExp >= FOUR_LEVEL_OF_EXP) {
            return 4;
        } else if (totalExp >= THREE_LEVEL_OF_EXP) {
            return 3;
        } else if (totalExp >= TWO_LEVEL_OF_EXP) {
            return 2;
        } else if (totalExp >= ONE_LEVEL_OF_EXP) {
            return 1;
        } else {
            return 0;
        }
    }

    public static enum LoopModes {

        ONLY_ONE("only_one"),
        DAY("day"),
        MONTH("month"),
        YEAR("year");

        private final String value;

        private LoopModes(String value) {
            this.value = value;
        }

        public String value() {
            return this.value;
        }
    }
}
